import Vanilla.UI

class Graph (Gadget):
	override MinSize as Size:
		get:
			return Size(200,100)
	
	override MaxSize as Size:
		get:
			return Size(200,100)
	
	def constructor (b as Box):
		super(b)
	
	values = array(double,20)
	current = 0
	public max = 1.0
	
	def Add (i as double):
		values[current] = i
		current++
		current = 0 if current >= values.Length
		
		while (max < i):
			max = max * 2
			print "max=${max}"
		
		Redraw()
	
	override def DrawContents (b as Brush):
		b.MoveTo(0,Size.Height)
		b.LineWidth = 2
		b.Colour = Colour.Red
		
		for i in range(values.Length):
			b.LineTo(i * (Size.Width / values.Length), Size.Height - ((values[i] / max) * Size.Height))
		
		b.Stroke()
		
		b.Draw(Rect(current*(Size.Width / values.Length) - Size.Width, 0, current*(Size.Width / values.Length), Size.Height))
		
		p = LinearGradient(current*(Size.Width / values.Length) - Size.Width, 0, current*(Size.Width / values.Length), 0)
		p.Add(0,Colour(0,1,0,0))
		p.Add(1,Colour(0,1,0,0.5))
		
		b.Pattern = p
		b.Fill()

class StatisticsWindow (MainWindow):
	Timer as UITimer

	OldPack as double = 0
	PackGraph as Graph
	PackStats as Label
	
	OldDrawTotal as double = 0
	OldDrawCount as double = 0
	DrawGraph as Graph
	DrawStats as Label

	def constructor ():
		super("UI Statistics")
		
		c = Column(self)
		PackGraph = Graph(c)
		PackStats = Label(c, "Please hold. Your call is important to us!")
		
		DrawGraph = Graph(c)
		DrawStats = Label(c, PackStats.Text)
		
		Size = MinSize
		
		Timer = UITimer(500) def ():
			PackGraph.Add(Gadget.packing_count - OldPack)
			PackStats.Text = "${Gadget.packing_count} total packs (${Gadget.packing_count - OldPack} in the last half second)"
			OldPack = Gadget.packing_count
			
			DrawGraph.Add((SDL.RealRedraws - OldDrawCount) / (SDL.TotalRedraws - OldDrawTotal))
			DrawStats.Text = "${SDL.RealRedraws} actually drawn of ${SDL.TotalRedraws} requests (total ratio: ${SDL.RealRedraws / SDL.TotalRedraws})"
			OldDrawTotal = SDL.TotalRedraws
			OldDrawCount = SDL.RealRedraws
			
	
	override def Close ():
		Timer.Stop()
		super()
